# -*- coding:utf-8 -*—
#第一步：调用pandas包
import time

import pandas as pd
import os
from pandas import DataFrame

def splitExcel(excelPath,operaField,splitOperate):
    '''
    按字段名去重
    :param excelPath:excel路径
    :param operaField:操作的列
    :param splitOperate:拆分标识符
    :return:
    '''
    #第二步：读入文件
    df = pd.read_excel(io=excelPath)
    # 第三步：获取class列表并去重
    dirStr, ext = os.path.splitext(excelPath)
    outpath = dirStr + '-splitColomnsXY.xlsx'
    # print(df)
    # todo:删除数据为空的行
    df[operaField] = df[operaField].fillna(999999)
    find_index = df[(df[operaField] == 999999)].index.tolist()
    df2=df.drop(find_index)

    # todo:插入两列
    # df2.insert(df2.shape[1], 'x', 0)
    # df2.insert(df2.shape[1], 'y', 0)
    #
    # print(df2[operaField].str.split(','))
    # print(df2[operaField].str.split(',').str)
    # df2['x'], df['y'] = df2[operaField].str.split(',').str

    for index, row in df2.iterrows():
        # print(row[operaField])  # 输出每行的索引值
        try:
            # x = row[operaField].split(',')[0]
            # y= row[operaField].split(',')[1]
            # row['经度'] = x
            # row['维度'] = y
            df2.loc[index, ['x']] = float(df2.loc[index, [operaField]].copy().values[0].split(',')[0])
            df2.loc[index, ['y']] = float(df2.loc[index, [operaField]].copy().values[0].split(',')[1])
            # print(df2)
        except Exception as e:
            # print(e)
            pass
        continue

    # df2.loc[:, 'x'] = df2[operaField].str.split(',')
    # df2.loc[:, 'y'] = df2[operaField].str.split(',')
    # print(df2[operaField].str.split(',',1).str)
    # print(df2[operaField].str.split(',')[0])

    print(df2)
    df2.to_excel(outpath)



if __name__ == '__main__':
    # todo：欧洲

    # path=r"F:\data\水电流域\2022.09.26预报数据对比\距平降雨出图\降雨和温度多年统计(1).xlsx"
    # path=r"E:\宁乡统计\所有道路.xlsx"
    path=r"E:\项目相关\13.可再生能源补贴\测试数据\无法成图分割坐标"
    splitOperate=';'
    # filedName="RVCD"
    # filedName="MONTH"
    # filedName="统一社会信用代码"
    operaField='拐点坐标'
    for root, dirs, files in os.walk(path):
        for file in files:
            print(file)     #文件名
            print(os.path.join(root, file))
            tempPath=os.path.join(root, file)
            # todo 分割excel
            splitExcel(tempPath,operaField,splitOperate)
